﻿
// Shows custom dialog using SharePoint Client JS OM
function TFSWITRibbonActionHandler(ActionId) 
{
    var script = document.createElement('script');
    //script.src = 'http: //code.jquery.com/jquery-1.9.0.js';
    script.src = 'TFSSPConnector/jquery-1.9.0.min.js';
    script.type = 'text/javascript';
    script.onreadystatechange = function () { TFSWITRibbonActionCallback(ActionId); };
    script.onload = function () { TFSWITRibbonActionCallback(ActionId); };
    document.getElementsByTagName('head')[0].appendChild(script); 
}

function TFSWITRibbonActionCallback(ActionId) 
{
    //var dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose('Loading Title', 'Loading Message Here...');
    //dlg.get_html().getElementsByTagName('TD')[1].innerHTML = 'Finalizing the operation. Almost done...'

    var canaryValue = document.getElementById('__REQUESTDIGEST').value;
    //request.SetRequestHeader("X-RequestDigest", canaryValue);
    var dlg = null;
    
    switch (ActionId) {
        case "tfswitconngroup.ValidateTFSConnection":
            dlg = myShowWaitScreenWithNoClose('Please Wait...', "Validating TFS Connection.");
            $.ajax({
                type: "POST",
                url: "TFSSPConnector/RibbonCustomControls.aspx/ValidateTFSConnection",
                data: "{}",
                beforeSend: function (request) { request.setRequestHeader("X-RequestDigest", canaryValue); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                xhrFields: { withCredentials: true },
                success: function (data) { dlg.close(); SuccessCallBack(data); },
                error: function (data) { dlg.close(); FailureCallBack(data); }
            });
            break;
        case "tfswitconngroup.CreateCustomLobSystem":
            dlg = myShowWaitScreenWithNoClose('Please Wait...', "Creating BDC External System.");
            $.ajax({
                type: "POST",
                url: "TFSSPConnector/RibbonCustomControls.aspx/CreateCustomLobSystem",
                data: "{}",
                beforeSend: function (request) { request.setRequestHeader("X-RequestDigest", canaryValue); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                xhrFields: { withCredentials: true },
                success: function (data) { dlg.close(); SuccessCallBack(data); },
                error: function (data) { dlg.close(); FailureCallBack(data); }
            });
            break;
        case "tfswitconngroup.ExportWITSchema":
            dlg = myShowWaitScreenWithNoClose('Please Wait...', "Exporting TFS WorkItem schema.");
            var options = {
                url: "TFSSPConnector/RibbonCustomControls.aspx?ActionId=" + ActionId,
                width: 200,
                height: 50,
                //allowMaximize: false,
                //showClose: false,
                dialogReturnValueCallback: function (dialogResult, data) { dlg.close(); myDialogCallback(dialogResult, data); }
            };
            SP.UI.ModalDialog.showModalDialog(options);
            break;
        default:
            alert("ActionId is unrecognized");
    }
}

function myShowWaitScreenWithNoClose(title, message) {
    return SP.UI.ModalDialog.$1I_1(title, message, false, false, null, null, 270, '12px 12px 4px 6px', 0); 
}

function myDialogCallback(dialogResult, data){  
    //dialogResult == SP.UI.DialogResult.OK

    if (data != null && data != "") alert(data);
    
    // this will refresh your page if dialogResult is successful  
    //SP.UI.ModalDialog.RefreshPage(dialogResult);
}

function SuccessCallBack(data) {
    alert(data.d);
}

function FailureCallBack(data) {
    alert(data.status + " : " + data.statusText);
}